#include "Mesh.h"
//Mesh::Mesh(VerticesPtr vp)
//	:m_vertices(vp),
//	m_idents(IndexsPtr()),
//	m_animation(false)
//{
//	m_mask=(VERTEX_MASK|COLOR_MASK);
//}
//
//Mesh::Mesh(VerticesPtr vp, IndexsPtr ip)
//	:m_vertices(vp),
//	m_idents(ip),
//	m_animation(false)
//{
//	m_mask=(VERTEX_MASK|INDEX_MASK|COLOR_MASK);
//}

Mesh::Mesh(string name,SubMeshPtr s,MeshUsage u)
	:Resource(name),
	m_subMeshs(s),
	m_usage(u),
	m_animData()
{
}

Mesh::Mesh(string name,SubMeshPtr s, Anims* anims, MeshUsage u)
	:Resource(name),
	m_subMeshs(s),
	m_usage(u),
	m_animData(anims)
{
}

Mesh::~Mesh(void)
{
	
}

Anims* Mesh::GetAnimData() const
{
	return m_animData;
}

void Mesh::CalcAnim(Bones* tmpBones)
{
	if(m_usage==MESH_STATIC)
		EXCEPTION("")


	
	/*fstream tmp("bone.txt");
	for(BonesIter biter=tmpBones->begin();biter!=tmpBones->end();biter++)
	{
		string data;
		data=boost::lexical_cast<string,float>((*biter)->pos.x);
		tmp<<data<<" ";
		data=boost::lexical_cast<string,float>((*biter)->pos.y);
		tmp<<data<<" ";
		data=boost::lexical_cast<string,float>((*biter)->pos.z);
		tmp<<data<<"\n";
		data=boost::lexical_cast<string,float>((*biter)->orientation.x);
		tmp<<data<<" ";
		data=boost::lexical_cast<string,float>((*biter)->orientation.y);
		tmp<<data<<" ";
		data=boost::lexical_cast<string,float>((*biter)->orientation.z);
		tmp<<data<<" ";
		data=boost::lexical_cast<string,float>((*biter)->orientation.w);
		tmp<<data<<"\n";
	}*/

	for(SubMeshIter siter=m_subMeshs->begin();siter!=m_subMeshs->end();siter++)
	{
		(*siter)->vertices->clear();
		Weights* tmpWeights=(*siter)->weights;
		for(VertsIter viter=(*siter)->verts->begin();viter!=(*siter)->verts->end();viter++)
		{
			XMFLOAT3 tmpPoint(0.0f,0.0f,0.0f);
			for(unsigned int i=0;i<(*viter)->count;i++)
			{
				Weight* tmpWeight=(*tmpWeights)[(*viter)->start+i];
				Bone* tmpBone=(*tmpBones)[tmpWeight->boneId]; 
			
				XMVECTOR tmpOrientation = XMLoadFloat4(&tmpBone->orientation);
				XMVECTOR tmpWeightPos = XMVectorSet(tmpWeight->pos.x, tmpWeight->pos.y, tmpWeight->pos.z, 0.0f);
				XMVECTOR tmpOrientationConjugate = XMQuaternionInverse(tmpOrientation);
			
				XMFLOAT3 rotatedPoint;
				XMStoreFloat3(&rotatedPoint, XMQuaternionMultiply(XMQuaternionMultiply(tmpOrientation, tmpWeightPos), tmpOrientationConjugate));
			
				XMFLOAT3 tmpPos=tmpBone->pos;
				tmpPoint.x+=(tmpPos.x+rotatedPoint.x)*tmpWeight->bias;
				tmpPoint.y+=(tmpPos.y+rotatedPoint.y)*tmpWeight->bias;
				tmpPoint.z+=(tmpPos.z+rotatedPoint.z)*tmpWeight->bias;
			}
		
			(*siter)->vertices->push_back(tmpPoint);

			//string data=boost::lexical_cast<string,float>(tmpPoint.x);
			//tmp<<data<<" ";
			//data=boost::lexical_cast<string,float>(tmpPoint.y);
			//tmp<<data<<" ";
			//data=boost::lexical_cast<string,float>(tmpPoint.z);
			//tmp<<data<<"\n";

		}
	}
}

Mesh::SubMeshPtr Mesh::GetSubMeshs()
{
	return m_subMeshs;
}

MeshUsage Mesh::GetMeshUsage() const
{
	return m_usage;
}
